package com.exercises;


import java.util.Arrays;

class Exercise17_10 {
    public static void main(String[] args) {
        System.out.println(majorityElement(new int[]{3, 3, 4}));
    }


    public static int majorityElement(int[] nums) {
        int key = -1;
        int num = 0;
        for (int n : nums) {
            if (num == 0) {
                key = n;

            }
            if (key == n) {
                num++;
            } else if (key != n) {
                num--;
            }
        }
        num = 0;
        for (int n : nums) {
            if (key == n) num++;
        }
        if (num * 2 > nums.length) {
            return key;
        }
        return -1;

    }


}

